$(function(){
    init_validates();
});

/*开启表单验证*/
function init_validates(){
    var groupid = $("#sale-tag-selectid").val();
    $("#tagGroupId").val(groupid);
    var $saleTagForm = $("#sale-tag-form");
    var icon = "<i class='zmdi zmdi-close-circle zmdi-hc-fw color-red'></i> ";
    var validator = $saleTagForm.validate({
        rules:{
            tagName:{
                required:true,
                minlength:1,
                maxlength:6,
                remote:{
                    url:ctx+"saleConfig/checkTagName",
                    type: "get",
                    dataType: "json",
                    data: {
                        tagName: function (){
                            return $("#tagName").val().trim();
                        },
                        tagGroupId: function (){
                            return $("#sale-tag-selectid").val().trim();
                        }
                    }
                }
            }
        },
        messages:{
            tagName:{
                required: icon + "请输入标签名称",
                minlength: icon + "标签名称长度1到6个字符",
                maxlength: icon + "标签名称长度1到6个字符",
                remote: icon + "标签名称已经存在"
            }
        }
    });
}

/*添加标签*/
function createTag() {
    var url=ctx+"saleConfig/createTag";
    var $newTagForm = $("#sale-tag-form");
    var validator = $newTagForm.validate();
    var flag = validator.form();
    if(flag){
        $.post(url,$("#sale-tag-form").serialize(),function (r) {
           if(r.code === 0){
               $("#sale-modal").modal('hide');
               afterAdd();
               $JuXue.n_success(r.msg);
           } else {
               $JuXue.n_danger(r.msg);
           }
        });
    }
}

/*添加完成后刷新标签*/
function afterAdd(){
    var uuid = $("#sale-tag-selectid").val();
    var url="saleConfig/chooseGroup";
    $("#sale-tag-lists").empty();
    $.post(url,{"uuid":uuid},function (r) {
        var data = r.data.list;
        if(data.length>0){
            $.each(data,function(index,val) {
                var button = "<li class='p-r-10 dropdown pull-left m-b-5' >" +
                    "<a class='p-t-3 p-r-10 p-b-3 p-l-10 dropdown-toggle btn' data-toggle='dropdown' style='border: 1px darkgrey solid'>"+val.tagName+"  <i class='fa fa-angle-down'></i></a>" +
                    "<ul class='dropdown-menu media-list pull-left animated fadeInDown'>" +
                    "<a href='javascript:;' uuid='"+val.tagUuid+"' name='"+val.tagName+"' onclick='updateTagName(this)'><li class='media p-l-15'><div class='media-right'><div class='media-body'><h5><i class='fa fa-pencil-square-o p-r-10'></i>修改名称</h5></div></div></li></a>" +
                    "<a href='javascript:;' uuid='"+val.tagUuid+"' onclick='deleteTag(this)'><li class='media p-l-15'><div class='media-right'><div class='media-body'><h5><i class='fa fa-trash-o p-r-15'></i>删除</h5></div></div></li></a>" +
                    "</ul></li>";
                $("#sale-tag-lists").append(button);
            });
        }
    });
}